﻿using System;
using System.Threading.Tasks;
using Cms.Core.Application.IService.RoleInfo;
using Cms.Core.Application.Service.RoleInfo.Dto;
using Cms.Core.AspNetMvc.Mvc;
using Cms.Core.Infrastructure;
using Cms.Core.Infrastructure.Extension;
using Cms.Core.Infrastructure.Ui;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Cms.Core.Infrastructure.Helps;

namespace Cms.Core.Web.Controllers
{
    /// <summary>
    /// 角色信息接口
    /// </summary>
    [Display("角色信息")]

    public class RoleController : WebApiController
    {

        private readonly IRoleService _roleService;


        public RoleController(IRoleService roleService) => _roleService = roleService;






        /// <summary>
        /// 异步添加或者更新角色
        /// </summary>
        /// <param name="input">角色Dto</param>
        /// <returns></returns>
        [Display("异步添加或者更新角色"), HttpPost]
        public async Task<AjaxResult> AddOrUpdateRoleAsync([FromBody]RoleDto input)
        {

            var result = await _roleService.UpdateOrAddRoleAsync(input);
            return result.ToAjaxResult();
        }

        /// <summary>
        /// 异步加载用户信息
        /// </summary>
        [Display("异步加载用户信息"), HttpPost]

        public async Task<AjaxResult> LoadRoleAsync([FromBody]JObject id)
        {

            var newId = id.ToObject("id")?.AsTo<Guid>();
            return (await _roleService.LoadRoleAsync(newId)).ToAjaxResult();
        }

        /// <summary>
        /// 异步得到角色分页数据
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        [Display("异步得到角色分页数据"), HttpPost]
        public async Task<PageData> GetRolePageDataAsync([FromBody]PageRequestData requestData)
        {

            var pageData = await _roleService.GetRolePageDataAsync(requestData);
            return pageData.ToPageData();
        }

        /// <summary>
        /// 异步软删除角色
        /// </summary>
        [Display("异步软删除角色"), HttpGet]

        public async Task<AjaxResult> SoftDeleteRoleAsync(Guid id)
        {

            return (await _roleService.SoftDeleteRoleAsync(id)).ToAjaxResult();

        }

        /// <summary>
        /// 异步得到角色选择列表项
        /// </summary>
        /// <returns></returns>
        [Display("异步得到角色选择列表项"), HttpGet]
        
        public async Task<AjaxResult> GetRoleSelectListItem()
        {
          return  (await _roleService.ToRoleSelectListItem()).ToAjaxResult();
        }
    }
}